import React from 'react';
import { BrowserRouter as Router, Route, NavLink, Switch, Redirect } from "react-router-dom";
import 'common/less/index.less'
import './App.less'
import routes from '@/router/router';
import 'common/iconfont/iconfont'
import 'common/js/http'
import { getUrlParams } from 'common/js/urlParams'
import { CONFIG } from 'api/config'
import { getWeChatOauth } from 'api/wechat'
import { getMerchantUserMsg } from 'api/seller'
import { getByAccessToken } from 'api/user'
import { observer, inject } from 'mobx-react'

@inject('mineMsg', 'merchantMsg')
@observer

class App extends React.Component {
  constructor(props) {
    super(props)
  }

  _getByAccessToken = () => {
    if (!localStorage.uuToken) return
    getByAccessToken().then((res) => {
      if (!res) return
      if (res.phoneNumber === null) {
        this.props.history.push(`/${CONFIG.APPID}/login`)
        return
      }
      localStorage.uuUserId = res.userId
      this.props.mineMsg.setMsg(res)
      // console.log(this.props.mineMsg.msg)
      this._getMerchantUserMsg(res.phoneNumber)
    })
  }

  _getMerchantUserMsg = (phone) => {
    let opts = {
      phone: phone
    }
    getMerchantUserMsg(opts).then((res) => {
      if (res) {
        localStorage.merchantMsg = JSON.stringify(res)
        localStorage.userMsg = JSON.stringify(res)
        this.props.merchantMsg.setMsg(res)
        // console.log(this.props.merchantMsg.msg)
      }
    })
  }

  componentDidMount() {
    if (!localStorage.uuOpenId) {
      getWeChatOauth()
    }
    this._getByAccessToken()
  }

  render() {
    // 自适应
    let fontSize = window.screen.width / 10 + 'px'
    document.getElementsByTagName('html')[0].style.fontSize = fontSize
    getUrlParams()
    // 登录 变量
    let token = localStorage.uuToken
    // let openId = localStorage.uuOpenId
    // let expiresIn  = localStorage.expiresIn
    let pathname = window.location.pathname
    let appid = pathname.split('/')[1]
    console.log(appid)
    // localStorage.uuToken = 'ee6d286fa480223f4915fd02f81e139b'
    // localStorage.uuOpenId = 'ovWZ00n6Oanr33EGFKizrA21dRjc'
    localStorage.expiresIn = JSON.stringify({ "validity": 1567045858, "validityDay": "7" })
    localStorage.userLocation = JSON.stringify({ "lng": 113.260962, "lat": 23.088623 })

    return (
      <Router>
        <div className="App">
          <footer className="tab">
            <NavLink to={`/${appid}/home`} activeClassName="home-active">首页</NavLink>
            <NavLink to={`/${appid}/seller`} activeClassName="seller-active">商家</NavLink>
            <NavLink to={`/${appid}/mine`} activeClassName="mine-active">我的</NavLink>
          </footer>

          <Switch>
            {
              routes.map((route, key) => {
                if (route.exact) {
                  return <Route key={key} exact path={route.path}
                    render={props => (
                      !route.auth ? <route.component {...props} routes={route.routes} /> : (token ? <route.component {...props} routes={route.routes} /> : <Redirect to={{
                        pathname: `/${appid}/login`,
                        fromState: pathname
                      }} />)
                    )}
                  />
                } else {
                  return <Route key={key} path={route.path}
                    // render = {props => (
                    //   <route.component {...props} routes = {route.routes} />
                    // )}
                    render={props => (
                      !route.auth ? <route.component {...props} routes={route.routes} /> : (token ? <route.component {...props} routes={route.routes} /> : <Redirect to={{
                        pathname: `/${appid}/login`,
                        fromState: pathname
                      }} />)
                    )}
                  />
                }
              })
            }
          </Switch>
        </div>
      </Router>
    )
  }

}

export default App;